package com.traveling.hztraveling.service.impl;

import com.traveling.hztraveling.entity.po.HzUser;
import com.traveling.hztraveling.entity.po.Password;
import com.traveling.hztraveling.entity.vo.Result;
import com.traveling.hztraveling.mapper.HzUserMapper;
import com.traveling.hztraveling.service.ProfileService;

import com.traveling.hztraveling.utils.AliyunUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;

@Service
@Slf4j
@RequiredArgsConstructor
public class ProfileServiceImpl implements ProfileService {

    private final HzUserMapper hzUserMapper;

    private final RedisTemplate redisTemplate;

//    private final AliOssUtil aliOssUtil;

    @Override
    @Transactional
    public Result<HzUser> getProfile(Integer userId){
//        HzUser user = hzUserMapper.selectById(userId);
        HzUser user = (HzUser) redisTemplate.opsForHash().get("userId",userId.longValue());
        if (user == null){
            return Result.fail("用户不存在");
        }
        log.info("查询用户信息成功,user:{}", user);
        return Result.success(user);
    }

    @Override
    @Transactional
    public Result<HzUser> updateProfile(HzUser hzUser){
        if (hzUser == null){
            return Result.fail("用户信息为空");
        }
        if (hzUser.getId() == null){
            return Result.fail("用户ID为空");
        }
        redisTemplate.opsForHash().put("userId",hzUser.getId().longValue(),hzUser);
        int i = hzUserMapper.updateById(hzUser);
        if (i == 0){
            return Result.fail("更新用户信息失败");
        }
        log.info("更新用户信息成功,user:{}", hzUser);
        return Result.success(hzUser);
    }

    @Override
    @Transactional
    public Result<HzUser> updatePassword(Integer userId, Password password){
        if (password == null){
            return Result.fail("密码信息为空");
        }
        HzUser user = hzUserMapper.selectById(userId);
        if (user == null){
            return Result.fail("用户不存在");
        }
        if (!user.getPassword().equals(password.getOldPassword())){
            return Result.fail("旧密码错误");
        }
        user.setPassword(password.getNewPassword());
        int i = hzUserMapper.updateById(user);
        if (i == 0){
            return Result.fail("更新用户信息失败");
        }
        log.info("更新用户信息成功,user:{}", user);
        return Result.success(user);

    }

    @Override
    @Transactional
    public Result updateAvatar(Integer userId, MultipartFile file){
        if (file == null){
            return Result.fail("文件为空");
        }
        String objectName = "userImageAvatar" + userId + ".jpg";
        try {
            File tempDir = Files.createTempDirectory("avatar-").toFile();
            File tempFile = new File(tempDir, "userImageAvatar" + userId + ".jpg");
            file.transferTo(tempFile);
            String s = AliyunUtils.updateAtavarImage(tempFile, objectName);
//            String s = aliOssUtil.upload(file.getBytes(),objectName);
            tempFile.delete();

//            HzUser hzUser = hzUserMapper.selectById(userId);
            HzUser hzUser = (HzUser) redisTemplate.opsForHash().get("userId",userId.longValue());
            if (hzUser == null){
                return Result.fail("用户不存在");
            }
            hzUser.setImage(s);
            redisTemplate.opsForHash().put("userId",hzUser.getId().longValue(),hzUser);
            hzUserMapper.updateById(hzUser);
            // 上传完成之后删除临时文件
            boolean deleted = tempDir.delete();
            if (!deleted) {
                // 可以记录日志提醒删除失败
            }
            return Result.success(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
